<?php

require_once(WSB_MODULES_PATH . 'sherpa/models/ProductGateway.php');
require_once(WSB_MODULES_PATH . 'sherpa/models/CategoryGateway.php');

class Sherpa_ProductListController extends WSB_Xtension_Extension
{

	/**
	 * @var string
	 */
	private $_mode;

	/**
	 * @var integer
	 */
	private $_page;

	/**
	 * @var integer
	 */
	private $_languageId;

	/**
	 * @var integer
	 */
	private $_catId;

	/**
	 * @var Sherpa_Model_Categories
	 */
	private $_categories;

	/**
	 * @return string
	 */
	public function renderForBackend()
	{
		return get_class($this);
	}

	/**
	 * Initialisierung
	 */
	public function init()
	{
		$this->_setMode();
		$this->_page = empty($_GET['page']) ? 1 : intval($_GET['page']);
		$this->_languageId = $this->getLanguageId();
		$this->_catId = $this->getParam('subcatId', 0);
		if(empty($this->_catId)) {
			$this->_catId = $this->getParam('catId', 0);
		}

		// Zeige Kategorien
		$gateway = new Sherpa_Model_CategoryGateway();
		$category = $gateway->fetchById($this->_catId, $this->_languageId);
		if($category instanceof Sherpa_Model_Category) {
			if(intval($category->getSubCatOverview()) > 0) {
				$this->_mode = 'categories';
				$this->_categories = $gateway->fetchByParentId($this->_languageId, $category->getId());
			}
		}

		// Seitentitel ersetzen
		$gateway = new Sherpa_Model_CategoryGateway();
		$tree = $gateway->fetchTree($this->getLanguageId());
		if(!empty($this->_catId)) {
			$tree = $tree->getNode($this->_catId);
			if($tree instanceof WSB_Model_SimpleTree) {
				$rootline = $tree->getRootline();
				$rootline->reverse();
				$titles = array();
				foreach($rootline AS $category) {
					$titles[] = $category->getName();
				}
				$this->_view->substitutePagetitle($titles);
			}
		}

	}

	/**
	 * Index Action
	 */
	public function indexAction()
	{
		switch($this->_mode) {
			case '3x3':
				$this->_index3x3();
				break;
			case '6x6':
				$this->_index6x6();
				break;
			case 'categories':
				$this->_categories();
				break;
		}
	}

	/**
	 * Index Action: Categories
	 */
	private function _categories()
	{
		$this->assignToView('categories', $this->_categories);
		$this->renderView('productlist/categories.phtml');
	}

	/**
	 * Index Action: 3x3
	 */
	private function _index3x3()
	{
		$gateway = new Sherpa_Model_ProductGateway();
		$gateway->setUsePaginator(true);
		$gateway->setItemCountPerPage(9); // Testwert

		$paginator = $gateway->fetchParents($this->_catId, $this->_languageId, $this->_page);

		// Hole Produkte und in mehreren Schritten deren Zusatzdaten
		// wie Preise, Bilder und Farben
		$products = new Sherpa_Model_Products($paginator->getCurrentItems());
		$products->loadParentProductPrices($this->getConfiguration()->currencyCode);
		$products->loadParentProductImages($this->_languageId);
		$products->loadParentProductColorApplications();

		$this->_view->paginator = $paginator;
		$this->_view->products = $products;
		$this->addContent($this->_view->render('productlist/3x3.phtml'));
	}

	/**
	 * Index Action: 6x6
	 */
	private function _index6x6()
	{
		$gateway = new Sherpa_Model_ProductGateway();
		$gateway->setUsePaginator(true);
		$gateway->setItemCountPerPage(36); // Testwert

		$paginator = $gateway->fetchParents($this->_catId, $this->_languageId, $this->_page);

		// Hole Produkte und in mehreren Schritten deren Zusatzdaten
		// wie Preise, Bilder und Farben
		$products = new Sherpa_Model_Products($paginator->getCurrentItems());
		$products->loadParentProductPrices($this->getConfiguration()->currencyCode);
		$products->loadParentProductImages($this->_languageId);

		$this->_view->paginator = $paginator;
		$this->_view->products = $products;
		$this->addContent($this->_view->render('productlist/6x6.phtml'));
	}

	/**
	 * Modes setzen
	 */
	private function _setMode()
	{
		$mode = empty($_GET['mode']) ? 0 : intval($_GET['mode']);
		switch($mode) {
			case '6x6':
				$this->_mode = '6x6';
				break;
			default:
			case '3x3':
				$this->_mode = '3x3';
				break;
		}
	}

}